package cn.wx.pet.controller;


import cn.wx.basic.annotation.PreAuthorize;
import cn.wx.basic.util.JsonResult;
import cn.wx.basic.util.PageList;

import cn.wx.pet.domain.PetType;
import cn.wx.pet.query.PetTypeQuery;
import cn.wx.pet.service.IPetTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petType")
@Api(value = "宠物类型的API",description="宠物类型相关的CRUD功能")
public class PetTypeController {

    @Autowired
    private IPetTypeService iPetTypeService;

    //使用get方式请求  查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<PetType> findAll(){
        return iPetTypeService.loadAll();
    }

    @GetMapping("/{id}")   //获取get请求里面的参数   查询一个
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "单独查询宠物",sn = "petType:get") //value后面用来做权限校验
    public PetType findById(@PathVariable("id") Long id ){
        return iPetTypeService.loadById(id);
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "删除宠物类型",sn = "petType:delete") //value后面用来做权限校验
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            iPetTypeService.remove(id);
           return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //添加和更新     //使用put方式请求
    @PutMapping
    @ApiOperation(value = "增加修改" )
    public JsonResult save(@RequestBody PetType petType){
        try {
            if(petType.getId()==null){
                iPetTypeService.save(petType);
            }else{
                iPetTypeService.update(petType);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //分页
    @PostMapping      //使用post方式请求
    @ApiOperation(value = "分页查询" )
    public PageList<PetType> queryPage(@RequestBody PetTypeQuery query){

        try {
            return  iPetTypeService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //批量删除
    @PatchMapping
    public JsonResult  patchDel(@RequestBody List<Long> ids){
        try {
            iPetTypeService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }


    //子集部门

    @GetMapping("/typeTree")
    public List<PetType> typeTree(){
       return iPetTypeService.typeTree();
    }

}
